package com.example.schooljava.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;

/**
 * <p>
 *  Mybatis-plus时间填充器
 * </p>
 *
 * @author LDH
 * @since 2022/08/11
 */

@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        // 获取当前时间
        Calendar cal= Calendar.getInstance();
        int y=cal.get(Calendar.YEAR); // 年
        int m=cal.get(Calendar.MONTH); // 月
        String s = null;
        if(m < 9)
            s = (y-1)+".9-"+y+".6";
        else
            s = y+".9-"+(y+1)+".6";
        log.info("start insert fill...");
        this.setFieldValByName("create_time", LocalDateTime.now(), metaObject);
        this.setFieldValByName("update_time", LocalDateTime.now(), metaObject);
        this.setFieldValByName("term",s,metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill...");
        this.setFieldValByName("update_time", LocalDateTime.now(), metaObject);
    }

}